package com.niiwoo.civet.mobile.util;

import com.niiwoo.civet.account.enums.OpenAccountMajorWay;
import com.niiwoo.civet.account.enums.OpenAccountWayEnum;
import com.niiwoo.civet.user.constants.LoginAccountErrorCode;
import com.niiwoo.tripod.provider.exception.BizException;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.Optional;

/**
 * 融360缓存服务
 */
@Component
@Slf4j
public class R360RedisService {

    @AllArgsConstructor
    @Getter
    public enum ThirdPart {
        R360("r360:account-register:", 1),
        CARD_NIU("cardniu:account-register:", 2),
        ORG("org:account-register:", 3);

        private String prefix;
        private Integer channel;

        public static ThirdPart of(int channel) {
            for (ThirdPart p : ThirdPart.values()) {
                if (p.getChannel() == channel) return p;
            }
            return null;
        }

    }

    @Autowired
    RedisTemplate<String, String> redisTemplate;

    /**
     * 通过 token 获取 userId
     *
     * @param token 由融360资产端生成
     */
    public String verifyTokenAndGetUserId(String token) {
        return verifyTokenAndGetUserId(token, ThirdPart.R360.getChannel());
    }

    /**
     * 通过 token 获取 userId
     *
     * @param token   令牌
     * @param channel 渠道
     * @return
     */
    public String verifyTokenAndGetUserId(String token, int channel) {
        log.info("第三方平台###令牌token={}, 渠道channel={}", token, channel);
        ThirdPart thirdPart = ThirdPart.of(channel);
        if (thirdPart == null) {
            log.error("第三方平台###未知channel={}", channel);
            throw new BizException(LoginAccountErrorCode.ACCESS_FORBID);
        }
        return Optional.ofNullable(token)
                .map(t -> {
                    String k = String.format("%s%s", thirdPart.getPrefix(), t);
                    String v = redisTemplate.opsForValue().get(k);
                    log.info("第三方平台###访问###k={}, v={}", k, v);
                    return v;
                })
                .orElseThrow(() -> new BizException(LoginAccountErrorCode.ACCESS_FORBID));
    }

    /**
     * 根据渠道获取开户方式
     *
     * @param majorWay
     * @param channel
     * @return
     */
    public OpenAccountWayEnum getOpenAccountWayByChannel(OpenAccountMajorWay majorWay, Integer channel) {
        ThirdPart thirdPart = ThirdPart.of(channel == null ? 1 : channel);
        if (majorWay == OpenAccountMajorWay.PERSON_BIND_BANK_CARD_REGISTER) {
            switch (thirdPart) {
                case R360:
                    return OpenAccountWayEnum.R360_PERSON_BIND_BANK_CARD_REGISTER;
                case CARD_NIU:
                    return OpenAccountWayEnum.CARD_NIU_PERSON_BIND_BANK_CARD_REGISTER;
                case ORG:
                    return OpenAccountWayEnum.ORG_BIND_BANK_CARD_REGISTER;
            }
        }
        if (majorWay == OpenAccountMajorWay.ACTIVATE) {
            switch (thirdPart) {
                case R360:
                    return OpenAccountWayEnum.R360_ACTIVATE;
                case CARD_NIU:
                    return OpenAccountWayEnum.CARD_NIU_ACTIVATE;
            }
        }
        if (majorWay == OpenAccountMajorWay.GROUP_ACCOUNT_CORRELATE) {
            switch (thirdPart) {
                case R360:
                    return OpenAccountWayEnum.R360_GROUP_ACCOUNT_CORRELATE;
                case CARD_NIU:
                    return OpenAccountWayEnum.CARD_NIU_GROUP_ACCOUNT_CORRELATE;
            }
        }
        return null;
    }
}
